package com.cwt_taxi_xa;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.cwt_taxi_xa.db.bll.ContactBiz;
import com.cwt_taxi_xa.model.ContactInfo;
import com.cwt_taxi_xa.util.PhoneApp;
import com.cwt_taxi_xa.util.ToastUtils;

import java.util.ArrayList;

public class ContactActivity extends CWTBaseActivity  implements
		OnScrollListener,OnTouchListener{

	private EditText et_search;
	private ImageView iv_clean;
	private ContactBiz biz;
	private ListView lvContacts;
	private ContactAdapter adapter;
	private ArrayList<ContactInfo> infos;
	private ArrayList<ContactInfo> filterContaInfos;
	public static Handler handler = null;
	private LinearLayout ll_not_msg;
	//	private LinearLayout ll_searchbar;
	public static boolean activityExist = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_contact);

		handler = new Handler(){
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				if(msg.what == 1005){
					ArrayList<ContactInfo> result = (ArrayList<ContactInfo>) msg.obj;
					if(result==null||result.size()<1){
//						ContactInfo c1 = new ContactInfo();
//						c1.setId(0);
//						c1.setName("庄群新");
//						c1.setPhone("13824123168");
//						c1.setType(3);
//						ContactInfo c2 = new ContactInfo();
//						c2.setId(1);
//						c2.setName("樊升军");
//						c2.setPhone("18602982800");
//						c2.setType(3);
//						result = new ArrayList<ContactInfo>();
//						result.add(c1);
//						result.add(c2);
					}
					boolean hasData = (result != null && result.size() != 0);
					showContace(hasData);
					if(hasData){
						infos = result;
						//						ll_searchbar.setVisibility(View.VISIBLE);
						lvContacts.setVisibility(View.VISIBLE);
						ll_not_msg.setVisibility(View.GONE);
						MainApplication.getInstance().infos = infos;
						adapter.changeData(infos);
					}
				}else if(msg.what == 1004){
					initData();
				}
			}
		};
		activityExist = true;
		biz = new ContactBiz(this);
		setupViews();
		addListener();
		//initData();
	}

	private void initData() {
		// TODO Auto-generated method stub
		adapter = new ContactAdapter(ContactActivity.this, infos);
		lvContacts.setAdapter(adapter);
		//		infos = CarCallApplication.getInstance().infos;
		//		if(infos != null && infos.size() != 0){
		//			handler.obtainMessage(1005, infos).sendToTarget();
		//		}else{
		biz.getContacts(handler);
		//		}
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		activityExist = false;
	}

	private void showContace(boolean hasData){
		if(hasData){
			ll_not_msg.setVisibility(View.GONE);
			lvContacts.setVisibility(View.VISIBLE);
		}else{
			ll_not_msg.setVisibility(View.VISIBLE);
			lvContacts.setVisibility(View.GONE);
		}
	}

	private void setupViews() {
		// TODO Auto-generated method stub
		lvContacts = (ListView) findViewById(R.id.lv);
		et_search = (EditText) findViewById(R.id.main_et_search);
		iv_clean = (ImageView) findViewById(R.id.main_iv_clean);
		ll_not_msg = (LinearLayout) findViewById(R.id.ll_not_msg);
		//		ll_searchbar = (LinearLayout) findViewById(R.id.ll_searchbar);
	}

	private void addListener() {
		// TODO Auto-generated method stub
		iv_clean.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				et_search.setText("");
			}
		});

		lvContacts.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
									int position, long id) {
				ContactInfo contact = adapter.getItem(position);
				String phoneNumber = contact.getPhone();
				int type = contact.getType();
				if(type != ContactInfo.TYPE_INCOMING){
					showCallDialog(phoneNumber);
				}else{
					ToastUtils.showToastForCustom("此号码禁止呼出！");
				}
			}
		});

		et_search.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
										  int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable cs) {
				// TODO Auto-generated method stub
				if (cs.length() <= 0) {
					boolean hasData = (infos != null && infos.size() != 0);
					showContace(hasData);
					adapter.changeData(infos);
				}else{
					filterContaInfos = biz.search(cs.toString(), infos);
					boolean hasData = (filterContaInfos != null && filterContaInfos.size() != 0);
					showContace(hasData);
					if(hasData){
						adapter.changeData(filterContaInfos);
					}
				}
			}
		});
	}

	public void showCallDialog(final String phone){
		Builder dialog = new AlertDialog.Builder(this);
		dialog.setTitle("温馨提示!").setIcon(android.R.drawable.ic_dialog_info)
				.setMessage("拨打："+phone).setCancelable(false)
				.setPositiveButton("拨打", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						dialog.dismiss();
						PhoneApp.call(ContactActivity.this, phone);
					}
				})
				.setNegativeButton("取消", null).create().show();
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
						 int visibleItemCount, int totalItemCount) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setStatus(int index, boolean status) {
		// TODO Auto-generated method stub

	}

	@Override
	public void sendMsg(int type, Object obj) {
		// TODO Auto-generated method stub

	}
}
